package com.jy.adscm.operation.mscyb.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.bean.system.JyCode;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.operation.mscyb.bean.JqbzPartsassembly;
import com.jy.adscm.operation.mscyb.bean.JqzzMachinedetail;
import com.jy.adscm.operation.mscyb.mapper.mysql.JqbzPartsassemblyMapper;
import com.jy.adscm.operation.mscyb.mapper.mysql.JqzzMachinedetailMapper;
import com.jy.adscm.operation.mscyb.service.JqzzMachinedetailService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.system.mapper.mysql.JyCodeMapper;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.swing.*;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2018-12-04
 */
@Service
public class JqzzMachinedetailServiceImpl extends ServiceImpl<JqzzMachinedetailMapper, JqzzMachinedetail> implements JqzzMachinedetailService {
     @Autowired
    JyUserMapper jyUserMapper;
     @Autowired
    JyCodeMapper jyCodeMapper;
     @Autowired
    JqbzPartsassemblyMapper jqbzPartsassemblyMapper;
     @Transactional
     @Override
    public boolean insertJqzzMachinedetail(JqzzMachinedetail jqzzMachinedetail, Token token) {
        //根据bjname查询到部件信息
        if (AdscmBaseUtils.getInstance().isNotEmpty(jqzzMachinedetail.getBjname(),null)){
            JqbzPartsassembly jqbzPartsassembly = jqbzPartsassemblyMapper.selectById(jqzzMachinedetail.getBjname());
            jqbzPartsassembly.setTaskfinishstate(1);
            jqbzPartsassembly.setIsoutstorage(1);
            jqbzPartsassembly.setMid(jqzzMachinedetail.getTid());
            jqbzPartsassembly.setTasknums(token.getUser().getId().toString());
            jqbzPartsassemblyMapper.updateById(jqbzPartsassembly);

            jqzzMachinedetail.setBjbatch(jqbzPartsassembly.getBatchnums());
            jqzzMachinedetail.setBjcode(jqbzPartsassembly.getTaskcode());
            jqzzMachinedetail.setBjrealname(jqbzPartsassembly.getResourcename());
            jqzzMachinedetail.setBjtype(jqbzPartsassembly.getTypenums());
            jqzzMachinedetail.setCreatetime(new Date());
            jqzzMachinedetail.setUserperson(token.getUser().getId().toString());
            boolean insert = super.insert(jqzzMachinedetail);
            return insert;
        }
         
        return false;
    }

    @Override
    public Boolean updateJqzzMachinedetail(JqzzMachinedetail jqzzMachinedetail) {
        return super.updateById(jqzzMachinedetail);
    }

    @Override
    public List<JqzzMachinedetail> selectByTid(String tid) {
         Wrapper<JqzzMachinedetail> wrapper =new EntityWrapper<>();
        wrapper.eq("TID",tid);
        List<JqzzMachinedetail> jqzzMachinedetails = super.selectList(wrapper);
        for (JqzzMachinedetail jqzzMachinedetail : jqzzMachinedetails) {
           if (AdscmBaseUtils.getInstance().isNotEmpty(jqzzMachinedetail.getUserperson(),null)){
               JyUser jyUser = jyUserMapper.selectById(jqzzMachinedetail.getUserperson());
               jqzzMachinedetail.setUserpersonName(jyUser.getName());
           }
           if (AdscmBaseUtils.getInstance().isNotEmpty(jqzzMachinedetail.getBjrealname(),null)){
               JyCode jyCode = jyCodeMapper.selectById(jqzzMachinedetail.getBjrealname());
               jqzzMachinedetail.setBjrealnameName(jyCode.getName());
           }
        }
        return jqzzMachinedetails;
    }

    @Override
    public List<JqbzPartsassembly> queryParts() {
        Wrapper<JqbzPartsassembly> wrapper =new EntityWrapper<>();
        wrapper.setSqlSelect("TID","RESOURCENAME");
        wrapper.eq("TASKFINISHSTATE",0);
        wrapper.eq("ISTASKEND",1);
        List<JqbzPartsassembly> jqbzPartsassemblies = jqbzPartsassemblyMapper.selectList(wrapper);
        for (JqbzPartsassembly jqbzPartsassembly : jqbzPartsassemblies) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(jqbzPartsassembly.getResourcename(),null)){
                JyCode jyCode = jyCodeMapper.selectById(jqbzPartsassembly.getResourcename());
                jqbzPartsassembly.setResourcenameName(jyCode.getName());
            }
        }
        return jqbzPartsassemblies;
    }
}
